/****************************************************************************
 *
 * Copyright (C) 2003-2010 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.r3000;

import java.sql.Timestamp;

import nfse.NetFSERecord;
import nfse.NetFSEUtil;
import nfse.gui.server.GUIRecordFactory;
import nfse.gui.server.RecordBuilderUtil;

public class R3000RecordFactory extends GUIRecordFactory<R3000Record> {

    public R3000RecordFactory() {
        RecordBuilderUtil.SQLField field = builder.addColumn(null,"id", RecordBuilderUtil.BIGINT);
        field = builder.addColumn("Start TS","startts",RecordBuilderUtil.TIMESTAMP);        
        field = builder.addColumn("Source IP","srcip" , RecordBuilderUtil.INET);
        field = builder.addColumn("Category","category",RecordBuilderUtil.VARCHAR,20);
        field = builder.addColumn("Type","type", RecordBuilderUtil.VARCHAR,20);
        field = builder.addColumn("URL Base","urlbase",RecordBuilderUtil.VARCHAR,255);
        field = builder.addColumn("URL","url",RecordBuilderUtil.TEXT);
    } 

    @Override
    protected void setValues(R3000Record record, int id) {
        int idx = -1;
        builder.setFieldValue(++idx, "" + id);
        builder.setFieldValue(++idx, "" + record.getStartTS());
        builder.setFieldValue(++idx, record.getSrcAddr());
        builder.setFieldValue(++idx, record.category);
        builder.setFieldValue(++idx, record.type);
        builder.setFieldValue(++idx, NetFSEUtil.prepareValueForInsert(record.urlBase));
        builder.setFieldValue(++idx, NetFSEUtil.prepareValueForInsert(record.url));
    }
    
    /***************************************************************************
     * Parse a comma-delimited R3000 record into a R3000Record data structure.
     * 
     * @param line
     *            The comma-delimitted R3000 record
     * @return The parsed R3000 record data structure
     */
    @Override
    public NetFSERecord generate(String line, int sensor) {
        try {
            R3000Record temp = new R3000Record();
            line = line.replaceAll("'", " ");
            if (line.endsWith("\\")) {
                line = line.substring(0, line.length() - 1);
            }
            temp.setInitialString(line);
            temp.setTypeID(typeID);
            temp.setTypeName(typeName);
            temp.setSensorID(sensor);
            String[] values = line.split(",");
            String date = values[2].replaceAll("/", "-");
            temp.setStartTS(Timestamp.valueOf(date + " " + values[3]));
            temp.setSrcIP(NetFSEUtil.convertIPtoLong(values[0]));
            temp.category = values[1];
            temp.type = values[4];
            int urlStartIdx = 5;
            while (!values[urlStartIdx].startsWith("http") && !values[urlStartIdx].startsWith("pattern"))
                urlStartIdx++;
            for (int i = urlStartIdx; i < values.length; i++) {
                if (i > urlStartIdx)
                    temp.url += ",";
                temp.url += values[i];
            }
            temp.setSensorID(sensor);
            
            // Comment this out unless debugging 
            //temp.setStartTS(new Timestamp(System.currentTimeMillis()));
            
            
            return temp;
        } catch (Exception E) {
            // E.printStackTrace();
            // System.out.println("R3000, BAD RECORD: '" + line + "'");
            return null;
        }
    }


    @Override
    public NetFSERecord generate() {
        return new R3000Record();
    }


  

}
